home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / Drivers / vc_2_2.lha / ParNet / Source / lock.asm < prev    next >
Encoding:
Assembly Source File  |  1994-11-02  |  3.1 KB  |  133 lines

  1. **
  2. ** $Header: SRC:CVSROOT/Vector/ParNet/lock.asm,v 1.1.1.1 1994/06/23 02:39:39 Barnard Exp $
  3. **
  4.  
  5. **
  6. ** This code was originally written by Matthew Dillon and put into Public Domain
  7. **
  8. ** All changes concerning the adaption of Matt's original code to the
  9. ** Vector Connection I/O board are © 1991-1994 by Henning Schmiedehausen
  10. ** All rights for this changes are reserved. The original code is Public Domain
  11. **
  12. ** This code is distributed with the expressed written permission of Matthew
  13. ** Dillon (Thank you very much, Matt)
  14. **
  15. **
  16.  
  17.         ;   long var[2] = { 0, 0 };
  18.         ;
  19.         ;   These routines provide fast exclusive
  20.         ;   locks.
  21.         ;
  22.         ;   LockAddr(&var[0]:A0)
  23.         ;   UnlockAddr(&var[0]:A0)
  24.         ;   TryLockAddr(&var[0]:A0)
  25.  
  26.         section __MERGED,DATA
  27.  
  28.         xref    _SysBase
  29.  
  30.         section text,CODE
  31.  
  32.         include "exec/types.i"
  33.         include "exec/ports.i"
  34.         include "exec/tasks.i"
  35.         include "exec/execbase.i"
  36.         include "exec/ables.i"
  37.         include "exec/memory.i"
  38.  
  39.         xref    _LVOWait
  40.         xref    _LVOForbid
  41.         xref    _LVOPermit
  42.         xref    _LVOSignal
  43.  
  44.         xdef    _LockAddr
  45.         xdef    _UnlockAddr
  46.         xdef    _TryLockAddr
  47.  
  48. _TryLockAddr:
  49.         move.l    4(sp),A0
  50.         moveq.l #0,D0
  51.  
  52.         bset.b    D0,4(A0)                ; attempt to gain lock
  53.         bne    .tla10            ; failure
  54.         moveq.l #1,D0
  55.         rts                ; success, return 1
  56. .tla10        moveq.l #-1,D0            ; failure, return -1
  57.         rts
  58.  
  59. _LockAddr:                    ; bit: 0     lock: A0
  60.         move.l    4(sp),A0
  61.         moveq.l #0,D0
  62.  
  63.         bset.b    D0,4(A0)                ; attempt to gain lock
  64.         bne    .la10            ; failure
  65.         rts                ; success, done, rts (FAST)
  66.  
  67. .la10        movem.l A2/A6,-(sp)             ; failure (SLOW) (BLOCK)
  68.         move.l    _SysBase,A6        ; A6 = SYSBase
  69.         FORBID
  70.         bset.b    D0,4(A0)                ; try again after FORBID
  71.         beq    .la20            ; got it!
  72.  
  73.         ;   Put linked list structure on stack
  74.  
  75.         move.w    D0,-(sp)                ; bit#    12(sp)
  76.         move.l    ThisTask(A6),-(sp)      ; task#    8(sp)
  77.         move.l    A0,-(sp)                ; &var     4(sp)
  78.         move.l    (A0),-(sp)              ; Next      (sp)
  79.         move.l    sp,(A0)                 ; (put at head of list)
  80.  
  81.         ;   Loop Wait/re-attempt lock
  82.  
  83. .la15        moveq.l #$10,D0         ; wait (semaphore signal)
  84.         jsr    _LVOWait(A6)
  85.  
  86.         move.w    12(sp),D0               ; try for lock
  87.         move.l    4(sp),A0
  88.         bset.b    D0,4(A0)
  89.         bne    .la15            ; loop until get it
  90.  
  91. .la16        cmp.l    (A0),sp                 ; unlink, find our node!
  92.         beq    .la18
  93.         move.l    (A0),A0
  94.         bra    .la16
  95. .la18        move.l    (sp),(A0)
  96.         add.w    #14,sp
  97. .la20
  98.         PERMIT
  99.         movem.l (sp)+,A2/A6
  100.         rts
  101.  
  102. _UnlockAddr:    move.l    4(sp),A0
  103.         moveq.l #0,D0
  104.  
  105.         bclr.b    D0,4(A0)                ; clear lock bit
  106.         move.l    (A0),D1                 ; anybody waiting?
  107.         beq    .ulrts            ; no, rts
  108.  
  109.         movem.l D2/A2/A6,-(sp)          ; yes, wake 'm all up
  110.         move.b    D0,D2            ; D2 = bit#
  111.         move.l    _SysBase,A6        ; A6 = SYSBase
  112.         FORBID
  113.  
  114.         move.l    (A0),D1                 ; get pointer again after FORBID
  115.         beq    .ul20            ; no, rts (close a window)
  116.  
  117. .ul10        move.l    D1,A2            ; A2 = node
  118.         cmp.b    13(A2),D2               ; waiting on our bit #?
  119.         bne    .ul18            ; no
  120.         move.l    8(A2),A1                ; yes, signal the node
  121.         moveq.l #$10,D0
  122.         jsr    _LVOSignal(A6)          ; signal EVERYONE waiting
  123. .ul18        move.l    (A2),D1                 ; next
  124.         bne    .ul10
  125.  
  126. .ul20
  127.         PERMIT
  128.         movem.l (sp)+,D2/A2/A6
  129. .ulrts        rts
  130.  
  131.         END
  132.  
  133.